//
//  LCAppPackagerController.m
//  AFNetworking
//
//  Created by 贺彦文 on 2021/3/24.
//

#import "LCAppPackagerController.h"
#import "LCAppPackagerCell.h"
#import "LCAppPackagerRequest.h"
#import "LCAppPackageEntity.h"

@interface LCAppPackagerController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray <LCAppPackageEntity *>*items;
@end

@implementation LCAppPackagerController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(self.view).offset(88);
        make.left.right.bottom.equalTo(self.view);
    }];
    [self request];
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [_tableView addGestureRecognizer:longPressGR];
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPressGR
{
    CGPoint point = [longPressGR locationInView:_tableView];
    NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point];
    LCAppPackagerCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
    if (longPressGR.state == UIGestureRecognizerStateBegan && indexPath) {
        NSString *appUrlString = [NSString stringWithFormat:@"https://www.pgyer.com/%@",self.items[indexPath.item].buildKey];
        [[UIPasteboard generalPasteboard] setString:appUrlString];
        [SVProgressHUD showInfoWithStatus:@"安装包下载地址已复制到剪贴板"];
        UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
        [feedBackGenertor impactOccurred];
    }
}
- (void)request{
    LCAppPackagerRequest *api = [[LCAppPackagerRequest alloc] initWithPage:self.page];
    [SVProgressHUD show];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD dismiss];
        NSArray *list = [NSArray yy_modelArrayWithClass:LCAppPackageEntity.class json: request.responseObject[@"data"][@"list"]];
        [self.items addObjectsFromArray:list];
        [self.tableView reloadData];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",request.responseString);
    }];
}

#pragma mark - UITableViewDelegate


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCAppPackagerCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LCAppPackagerCell class]) forIndexPath:indexPath];
    cell.entity = self.items[indexPath.item];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 84.f;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *itmsServiceUrlString = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=https://www.pgyer.com/app/plist/%@",self.items[indexPath.item].buildKey];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:itmsServiceUrlString]];
}
- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.delegate = self;
        tableView.dataSource = self;
        [tableView registerClass:[LCAppPackagerCell class] forCellReuseIdentifier:NSStringFromClass(LCAppPackagerCell.class)];
        _tableView = tableView;
    }
    return _tableView;
}
- (NSMutableArray <LCAppPackageEntity *>*)items {
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}
@end
